import req from "./requst";
const apiList: object = import.meta.glob("./list/*.ts", { eager: true });

let api: { [key: string]: ApiMap } = {};
Object.entries(apiList).forEach((key) => {
  api = {
    ...api,
    [key?.[0]?.replace(/(.*\/)*([^.]+).*/gi, "$2")]: key?.[1].default,
  };
});
export const https = ({ mode, name, data = {} }: api): Promise<Res> | Res => {
  try {
    // 获取路径
    const [url = "", method = "", headers = {}] = api[mode].get(name);
    return req.request("api/" + url, method, data, headers) as Promise<Res>;
  } catch (error) {
    uni.showToast({
      title: "api数据格式错误",
      icon: "none",
    });
    return {
      data: {},
      code: 0,
    };
  }
};
